home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / mhis020.zip / WILDCARD.MH < prev   
Text File  |  1996-09-20  |  3KB  |  144 lines

  1. // This file defines functions for performing regular expression searches on
  2. // strings.
  3. //
  4. // Functions:
  5. //
  6. // bool         matchWildcard   (string: regexp, string: toCompare)
  7. //
  8. //
  9. //
  10. // Description of matchWildcard ()
  11. //
  12. //      Compares two strings. The first string may contain wildcards.
  13. //      Recognised wildcards are:
  14. //
  15. //              *       match any 0 or more characters
  16. //              !       match anything but the following character
  17. //              [...]   match any of the characters within the brackets
  18. //              ?       match any one character
  19. //
  20. //      Returns True if the strings match, False otherwise.
  21. //
  22.  
  23.  
  24. #ifndef __WILDCARD_MH
  25. #define __WILDCARD_MH
  26.  
  27. #ifndef __STRING_MH
  28. #include "string.mh"
  29. #endif
  30.  
  31. #ifndef __GENERAL_MH
  32. #include "general.mh"
  33. #endif
  34.  
  35.  
  36. int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2);
  37.  
  38. // This routine is called by matchWildcard. It returns the number of characters
  39. // matched.
  40.  
  41. int matchSubstr (string: theString, string: wildcards, int: i1, Ref int: i2) {
  42.   char: curChar;
  43.  
  44.   curChar := wildcards [i2];
  45.  
  46.   if ((curChar = '?')
  47.     or (curChar = theString [i1])) {
  48.     i2 := i2 + 1;
  49.     return 1;
  50.     }
  51.   else if (curChar = '\\') {
  52.     i2 := i2 + 2;
  53.     if (wildcards [i2-1] = theString [i1]) {
  54.       return 1;
  55.       }
  56.     else return 0;
  57.     }
  58.   else if (curChar = '!') {
  59.     i2 := i2 + 1;
  60.     if (matchSubstr (theString, wildcards,i1,i2) = 0) {
  61.       return 1;
  62.       }
  63.     else return 0;
  64.     }
  65.   else if (curChar = '*') {
  66.     int: temp1, temp2, r;
  67.  
  68.     if (i2 = strlen (wildcards)) {
  69.       i2 := i2 + 1;
  70.       return strlen (theString) - i1 + 1; 
  71.       };
  72.  
  73.     for (temp1 := i1; temp1 <= strlen (theString); temp1 := temp1 + 1) {
  74.       temp2 := i2 + 1;
  75.       r := matchMultiple (theString, wildcards, temp1, temp2);
  76.       if (r = strlen (theString) - temp1 + 1) {
  77.         i2 := temp2;
  78.         return r + temp1 - i1;
  79.         };
  80.       };
  81.     return 0;
  82.     }
  83.   else if (curChar = '[') {
  84.     int: found, tmp, temp1;
  85.     found := 0;
  86.     i2 := i2 + 1;
  87.     for (; wildcards [i2] <> ']'; ) {
  88.       tmp := (wildcards [i2] = theString [i1]);
  89.       i2 := i2 + 1;
  90. //      tmp := matchSubstr (theString, wildcards, i1, i2);
  91.       if (tmp > found) {
  92.         found := tmp;
  93.         };
  94.       };
  95.     i2 := i2 + 1;
  96.     return found;
  97.     }
  98.   else {
  99.     i2 := i2 + 1;
  100.     return 0;
  101.     };
  102.   }
  103.  
  104. int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2) {
  105.   int: temp1, result, len;
  106.   len := strlen (theString);
  107.  
  108.   for (temp1 := i1; temp1 <= len; ) {
  109.     result := matchSubstr (theString, wildcards, temp1, i2);
  110.     if (result) {
  111.       temp1 := temp1 + result;
  112.       }
  113.     else return temp1 - i1;
  114.     };
  115.   return temp1 - i1;
  116.   }
  117.  
  118. // MatchWildcard compares a string with another string containing wildcards, and
  119. // determines if the two match.
  120.  
  121. bool matchWildcard (string: theString, string: wildcards) {
  122.   int: i1, i2, result;
  123.  
  124.   if (strlen (wildcards) < 2 ) {
  125.     if (strlen (wildcards) = 0) return False;
  126.     if ((strlen (wildcards) = 1) 
  127.       and (wildcards [1] = '*')) return True;
  128.     };
  129.  
  130.   i2 := 1;
  131.   if (matchMultiple (theString, wildcards, 1, i2) = strlen (theString))
  132.     if ((i2 >= strlen (wildcards))
  133.          ) {
  134.       if (i2 = strlen (wildcards)) {
  135.         return (wildcards [i2] = '*');
  136.         };
  137.       return True;
  138.       }
  139.  
  140.   return False;
  141.   }
  142.  
  143. #endif
  144.